<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import usePeople from '@renderer/composables/usePeople'
import { ApiEnum } from '@renderer/enum/ApiEnum'
import { http } from '@renderer/plugins/axios'
import { Check } from '@element-plus/icons-vue'
const route = useRouter()
const pageQuery = route.currentRoute.value.query
const selectAccountList = ref<any[]>([])
const selectOverrideList = ref<any[]>([])
const selectReportList = ref<any[]>([])
const selectBudgetList = ref<any[]>([])
const selectInfoList = ref<any[]>([])
const selectProjectList = ref<any[]>([])
const selectSaleList = ref<any[]>([])
const selectRedivList = ref<any[]>([])
const selectWebSetList = ref<any[]>([])
const selectUtilList = ref<any[]>([])
const selectWebList = ref<any[]>([])
const superCheck = ref(false)
const accountList = ref([
  '公司账本查看权',
  '意向金账本查看权',
  '欢乐基金账本查看权',
  '借支账本查看权',
  '分红账本查看权',
  '仓库账本查看权',
  '其他账本查看权',
  '项目账本查看权',
  '首页简报查看权',
  '账本分类修改权',
  '记账权限',
])
const overrideList = ref(['超控申请修改权', '超控帐目驳回权'])
const reportList = ref([
  '财务简报查看权',
  '利润报表查看权',
  '成本报表查看权',
  '提成报表查看权',
  '项目款报表查看权',
  '供应商报表查看权',
  '工人报表查看权',
  '超控报表查看权',
  '退单报表查看权',
  '设计部报表查看权',
  '工程部报表查看权',
])
const budgetList = ref([
  '成本明细查看权',
  '成本明细修改权',
  '预算明细查看权',
  '预算明细修改权',
  '全部预算查看权',
  '预算模板管理权',
])
const infoList = ref(['资讯查看权'])
const projectList = ref([
  '账本信息查看权',
  '项目文件查看权',
  '项目文件管理权',
  '项目文件下载权',
  '项目收款查看权',
  '财务信息查看权',
  '材料信息查看权',
  '施工任务查看权',
  '施工任务管理权',
  '统计报表查看权',
  '项目延期查看权',
  '备忘录添加权',
  '全部项目查看权',
])
const saleList = ref(['售后添加权', '售后删除权', '全部售后查看权'])
const redivList = ref([
  '普通账本记账审批权',
  '项目账本记账审批权',
  '账目删除审批权',
  '关联预算审批权',
  '项目账本信息审批权',
  '超控审批权',
  '付款操作权',
  '项目延期审批权',
  '售后延期审批权',
])
const webSetList = ref([
  '核算方式修改权',
  '损耗系数修改权',
  '材料规格修改权',
  '预计成本设置修改权',
  '身份设置修改权',
  '身份权限模板设置修改权',
  '审批设置修改权',
  '服务团队设置修改权',
  '提成设置修改权',
  '超控设置修改权',
  '预算标题/备注修改权',
])
const utilList = ref([
  '个人资料修改权',
  '公司账号信息查看权',
  '账户现金查看权',
  '账户现金管理权',
  '库存明细查看权',
  '商品清仓预订权',
  '全部商品清仓预订修改权',
  '员工管理列表查看权',
  '员工管理详情查看权',
  '支出对象管理列表查看权',
  '支出对象管理详情查看权',
  '人员管理修改权',
  '仓库商品修改权',
  '共享文档查看权',
  '共享文档管理权',
  '共享文档下载权',
  '总部返点修改权',
])
const webList = ref(['流水数据导出权', '库存数据导出权', '员工权限设置修改权'])

const deleteArray = (deleteIndex, deleteArr) => {
  deleteIndex
    .sort((a, b) => b - a)
    .forEach((index) => {
      if (index !== -1) {
        deleteArr.value.splice(index, 1)
      }
    })
}
const initData = async () => {
  let userRuleInfo
  if (pageQuery.userType == 'people') {
    const { userInfo } = await http.request<{ userInfo: UserModel; companyInfo: CompanyModel }>({
      url: ApiEnum.CURRENT_USER,
    })
    userRuleInfo = userInfo
  } else {
    userRuleInfo = await getPersonDetail({ id: pageQuery.userId })
  }
  superCheck.value = userRuleInfo.super_check == 1 && pageQuery.isDisabled ? true : false
  const { companyInfo } = await http.request<{ userInfo: UserModel; companyInfo: CompanyModel }>({
    url: ApiEnum.CURRENT_USER,
  })
  if (companyInfo.check_doc == 1) {
    deleteArray(
      ['共享文档查看权', '共享文档管理权', '共享文档下载权'].map((item) => utilList.value.indexOf(item)),
      utilList,
    )
  }
  if (companyInfo.check_work == 1) {
    deleteArray(
      [
        '账本信息查看权',
        '项目文件查看权',
        '项目文件管理权',
        '项目文件下载权',
        '项目收款查看权',
        '财务信息查看权',
        '材料信息查看权',
        '施工任务查看权',
        '施工任务管理权',
        '统计报表查看权',
        '项目延期查看权',
        '备忘录添加权',
        '全部项目查看权',
      ].map((item) => projectList.value.indexOf(item)),
      projectList,
    )
    deleteArray(
      ['设计部报表查看权', '工程部报表查看权'].map((item) => reportList.value.indexOf(item)),
      reportList,
    )
    deleteArray(
      ['项目延期审批权'].map((item) => redivList.value.indexOf(item)),
      redivList,
    )
  }
  if (companyInfo.check_after == 1) {
    deleteArray(
      ['售后添加权', '售后删除权', '全部售后查看权'].map((item) => saleList.value.indexOf(item)),
      saleList,
    )
    deleteArray(
      ['售后延期审批权'].map((item) => redivList.value.indexOf(item)),
      redivList,
    )
  }
  if (companyInfo.check_refund == 1) {
    deleteArray(
      ['总部返点修改权'].map((item) => utilList.value.indexOf(item)),
      utilList,
    )
  }
  if (companyInfo.check_record == 1) {
    deleteArray(
      ['流水数据导出权'].map((item) => webList.value.indexOf(item)),
      webList,
    )
  }
  let selectBaseAllList = [
    accountList,
    overrideList,
    reportList,
    budgetList,
    infoList,
    projectList,
    saleList,
    redivList,
    webSetList,
    utilList,
    webList,
  ]
  let selectAllList = [
    selectAccountList,
    selectOverrideList,
    selectReportList,
    selectBudgetList,
    selectInfoList,
    selectProjectList,
    selectSaleList,
    selectRedivList,
    selectWebSetList,
    selectUtilList,
    selectWebList,
  ]
  if (userRuleInfo.rule != '*') {
    userRuleInfo.rule.split(',').forEach((rule) => {
      selectBaseAllList.forEach((ruleFiled, ruleIndex) => {
        if (ruleFiled.value.includes(rule)) {
          selectAllList[ruleIndex].value.push(rule)
        }
      })
    })
  } else {
    selectBaseAllList.forEach((ruleFiled, ruleIndex) => {
      ruleFiled.value.forEach((rule) => {
        selectAllList[ruleIndex].value.push(rule)
      })
    })
  }
}
const handleSaveRule = async () => {
  let ruleList = [] as any[]
  let selectAllList = [
    selectAccountList,
    selectOverrideList,
    selectReportList,
    selectBudgetList,
    selectInfoList,
    selectProjectList,
    selectSaleList,
    selectRedivList,
    selectWebSetList,
    selectUtilList,
    selectWebList,
  ]
  selectAllList.forEach((item) => {
    ruleList = ruleList.concat([item.value])
  })
  await setPersonRule({
    id: pageQuery.userId,
    rule: [...new Set(ruleList)].join(','),
  })
}
const { getPersonDetail, setPersonRule } = usePeople()
await initData()
</script>

<template>
  <main
    class="bg-white flex flex-col py-4 px-3 gap-2 relative rounded-md overflow-auto nodrag"
    style="height: -webkit-fill-available">
    <div class="flex flex-col gap-3">
      <span class="text-black font-bold text-base">账本</span>
      <el-checkbox-group v-model="selectAccountList" class="ml-5">
        <el-checkbox
          labelSize="13"
          labelColor="#222222"
          v-for="(item, index) in accountList"
          :key="index"
          :label="item"
          :name="item"
          :disabled="superCheck">
        </el-checkbox>
      </el-checkbox-group>
    </div>
    <div class="flex flex-col gap-3">
      <span class="text-black font-bold text-base">超控</span>
      <el-checkbox-group v-model="selectOverrideList" class="ml-5">
        <el-checkbox
          labelSize="13"
          labelColor="#222222"
          v-for="(item, index) in overrideList"
          :key="index"
          :label="item"
          :name="item"
          :disabled="superCheck">
        </el-checkbox>
      </el-checkbox-group>
    </div>
    <div class="flex flex-col gap-3">
      <span class="text-black font-bold text-base">报表</span>
      <el-checkbox-group v-model="selectReportList" class="ml-5">
        <el-checkbox
          labelSize="13"
          labelColor="#222222"
          v-for="(item, index) in reportList"
          :key="index"
          :label="item"
          :name="item"
          :disabled="superCheck">
        </el-checkbox>
      </el-checkbox-group>
    </div>
    <div class="flex flex-col gap-3">
      <span class="text-black font-bold text-base">预算</span>
      <el-checkbox-group v-model="selectBudgetList" class="ml-5">
        <el-checkbox
          labelSize="13"
          labelColor="#222222"
          v-for="(item, index) in budgetList"
          :key="index"
          :label="item"
          :name="item"
          :disabled="superCheck">
        </el-checkbox>
      </el-checkbox-group>
    </div>
    <div class="flex flex-col gap-3">
      <span class="text-black font-bold text-base">资讯</span>
      <el-checkbox-group v-model="selectInfoList" class="ml-5">
        <el-checkbox
          labelSize="13"
          labelColor="#222222"
          v-for="(item, index) in infoList"
          :key="index"
          :label="item"
          :name="item"
          :disabled="superCheck">
        </el-checkbox>
      </el-checkbox-group>
    </div>
    <div class="flex flex-col gap-3">
      <span class="text-black font-bold text-base">项目管理</span>
      <el-checkbox-group v-model="selectProjectList" class="ml-5">
        <el-checkbox
          labelSize="13"
          labelColor="#222222"
          v-for="(item, index) in projectList"
          :key="index"
          :label="item"
          :name="item"
          :disabled="superCheck">
        </el-checkbox>
      </el-checkbox-group>
    </div>
    <div class="flex flex-col gap-3">
      <span class="text-black font-bold text-base">售后管理</span>
      <el-checkbox-group v-model="selectSaleList" class="ml-5">
        <el-checkbox
          labelSize="13"
          labelColor="#222222"
          v-for="(item, index) in saleList"
          :key="index"
          :label="item"
          :name="item"
          :disabled="superCheck">
        </el-checkbox>
      </el-checkbox-group>
    </div>
    <div class="flex flex-col gap-3">
      <span class="text-black font-bold text-base">待审审核</span>
      <el-checkbox-group v-model="selectRedivList" class="ml-5">
        <el-checkbox
          labelSize="13"
          labelColor="#222222"
          v-for="(item, index) in redivList"
          :key="index"
          :label="item"
          :name="item"
          :disabled="superCheck">
        </el-checkbox>
      </el-checkbox-group>
    </div>
    <div class="flex flex-col gap-3">
      <span class="text-black font-bold text-base">系统设置</span>
      <el-checkbox-group v-model="selectWebSetList" class="ml-5">
        <el-checkbox
          labelSize="13"
          labelColor="#222222"
          v-for="(item, index) in webSetList"
          :key="index"
          :label="item"
          :name="item"
          :disabled="superCheck">
        </el-checkbox>
      </el-checkbox-group>
    </div>
    <div class="flex flex-col gap-3">
      <span class="text-black font-bold text-base">常用功能</span>
      <el-checkbox-group v-model="selectUtilList" class="ml-5">
        <el-checkbox
          labelSize="13"
          labelColor="#222222"
          v-for="(item, index) in utilList"
          :key="index"
          :label="item"
          :name="item"
          :disabled="superCheck">
        </el-checkbox>
      </el-checkbox-group>
    </div>
    <div class="flex flex-col gap-3">
      <span class="text-black font-bold text-base">系统功能</span>
      <el-checkbox-group v-model="selectWebList" class="ml-5">
        <el-checkbox
          labelSize="13"
          labelColor="#222222"
          v-for="(item, index) in webList"
          :key="index"
          :label="item"
          :name="item"
          :disabled="superCheck">
        </el-checkbox>
      </el-checkbox-group>
    </div>
    <div class="successBtn nodrag cursor-pointer" @click="handleSaveRule">
      <el-icon color="#ffffff" size="26">
        <Check />
      </el-icon>
    </div>
  </main>
</template>

<style lang="scss" scoped>
.successBtn {
  position: fixed;
  bottom: 100px;
  right: 15px;
  width: 45px;
  height: 45px;
  background: linear-gradient(142deg, #ff9600 0%, #ffb242 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 50%;
}
.el-checkbox {
  display: flex;
}
</style>
